package com.ali.service.impl;

import com.ali.config.DBUserDetailsManager;
import com.ali.dao.UserMapper;
import com.ali.entity.User;
import com.ali.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private DBUserDetailsManager dbUserDetailsManager;

    @Override
    public void saveUser(Map<String, Object> param) {
        String username = (String) param.get("username");
        String password = (String) param.get("password");

        //创建密码编码器
        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        //创建用户
        UserDetails userDetails = org.springframework.security.core.userdetails.User.withUsername(username).password(password).passwordEncoder(passwordEncoder::encode).build();
        //管理用户
        dbUserDetailsManager.createUser(userDetails);
    }
}
